<?php
namespace app\admin\model;

use think\Config;
use think\Model;
use think\Db;
use think\Session;

class UserModel extends Model
{
	/**
	*	会员列表
	*/
	public function getUser($data)
	{
		$where = [];

		/* 会员姓名 */
		$u_name = empty($data['u_name']) ? '' : $data['u_name'];
		$u_name = trim($u_name);
		if(!empty($u_name)){
			$where['u_name'] = $u_name;
		}

		/* 身份证号 */
		$u_card = empty($data['u_card']) ? '' : $data['u_card'];
		$u_card = trim($u_card);
		if(!empty($u_card)){
			$where['u_card'] = $u_card;
		}

		/* 手机号码 */
		$u_tel = empty($data['u_tel']) ? '' : $data['u_tel'];
		$u_tel = trim($u_tel);
		if(!empty($u_tel)){
			$where['u_tel'] = $u_tel;
		}

		/* 店铺名称 */
		$u_store = empty($data['u_store']) ? '' : $data['u_store'];
		$u_store = trim($u_store);
		if(!empty($u_store)){
			$where['u_store'] = $u_store;
		}

		/* 店铺名称 */
		$keyword = empty($data['keyword']) ? '' : $data['keyword'];
		$keyword = trim($keyword);
		if(!empty($keyword)){
			$where['u_address'] = ['like',"%$keyword%"];
		}

		/* 会员类型 */
		$u_type = empty($data['u_type']) ? '' : $data['u_type'];
        if(!empty($u_type)){
        	$where['u_type'] = $u_type;
        }

        $where['u_type'] = ['<=',2];

		$result = Db::table('sz_user')->where($where)->paginate(20,false,['query' => request()->param()]);

		return $result;
	}


	/**
	*	合伙人列表
	*/
	public function getPartner($data)
	{
		$where = [];

		/* 会员姓名 */
		$u_name = empty($data['u_name']) ? '' : $data['u_name'];
		$u_name = trim($u_name);
		if(!empty($u_name)){
			$where['u_name'] = $u_name;
		}

		/* 身份证号 */
		$u_card = empty($data['u_card']) ? '' : $data['u_card'];
		$u_card = trim($u_card);
		if(!empty($u_card)){
			$where['u_card'] = $u_card;
		}

		/* 手机号码 */
		$u_tel = empty($data['u_tel']) ? '' : $data['u_tel'];
		$u_tel = trim($u_tel);
		if(!empty($u_tel)){
			$where['u_tel'] = $u_tel;
		}

		/* 店铺名称 */
		$u_store = empty($data['u_store']) ? '' : $data['u_store'];
		$u_store = trim($u_store);
		if(!empty($u_store)){
			$where['u_store'] = $u_store;
		}

		/* 店铺名称 */
		$keyword = empty($data['keyword']) ? '' : $data['keyword'];
		$keyword = trim($keyword);
		if(!empty($keyword)){
			$where['u_address'] = ['like',"%$keyword%"];
		}

		/* 会员类型 */
		$where['u_type'] = 3;

		$result = Db::table('sz_user')->where($where)->paginate(20,false,['query' => request()->param()]);

		return $result;
	}

	/**
	*	添加会员
	*/
	public function addUser($data)
	{
		if(!empty($data)){

			// 将发布时间转为时间戳
	        $data['registr_time']    = strtotime(date('Y-m-d H:i',time()));

	        // 默认会员头像
	        $data['u_icon'] = '/static/img/headicon.png';

	        $result = Db::table('sz_user')->insert($data);

	        return $result;
		}
	}

	/**
	*	获取单个会员信息
	*/
	public function getOne($id)
	{
		if($id){
			$result = Db::table('sz_user')->where('id',$id)->find();
			return $result;
		}
	}

	/**
	*	修改会员信息
	*/
	public function editUser($id,$data)
	{
		if(!empty($data)){

			// 将发布时间转为时间戳
	        $data['registr_time']    = strtotime($data['registr_time']);

	        // 默认会员头像
	        $data['u_icon'] = '/static/img/headicon.png';

	        $result = Db::table('sz_user')->where('id',$id)->update($data);

	        return $result;
		}
	}

	/**
	*	获取单个合伙人信息
	*/
	public function getPartnersOne($id)
	{
		if($id){
			$result = Db::table('sz_user')->where('id',$id)->find();
			return $result;
		}
	}

	/**
	*	修改合伙人信息
	*/
	public function editPartner($id,$data)
	{
		if(!empty($data)){

			// 将发布时间转为时间戳
	        $data['registr_time']    = strtotime($data['registr_time']);

	        // 默认会员头像
	        $data['u_icon'] = '/static/img/headicon.png';

	        $result = Db::table('sz_user')->where('id',$id)->update($data);

	        return $result;
		}
	}


	/**
	*	删除会员信息
	*/
	public function deleteUser($id)
	{
		if(!empty($id)){
			$result = Db::table('sz_user')->where('id',$id)->delete();
			return $result;

		}else{

			return false;
		}
	}

	/**
	*	获取每个会员的收货地址列表
	*/
	public function getUsersAddress($uid)
	{
		$result = Db::table('sz_address')->alias('a')
				->join('sz_user u','a.uid=u.id','left')
				->field('a.*,u.u_name')
				->where('a.uid',$uid)
				->paginate(20,false,['query' => request()->param()]);
		
		return $result;
	}

	/**
	*	获取每个收货地址
	*/
	public function getAddress($id)
	{
		$result = Db::table('sz_address')->alias('a')
				->join('sz_user u','a.uid=u.id','left')
				->field('a.*,u.u_name')
				->where('a.id',$id)
				->find();

		return $result;
	}

	/**
	*	修改每个收货地址
	*/
	public function editUserAddress($id,$data)
	{
		if(!empty($data)){

			// 将发布时间转为时间戳
	        $data['addtime']    = strtotime($data['addtime']);

	        $result = Db::table('sz_address')->where('id',$id)->update($data);

	        return $result;
		}
	
		return false;
	}

	/**
	*  删除会员的收货地址
	*/
	public function deleteAddress($id)
	{
		if($id){
			$res = Db::table('sz_address')->where('id',$id)->delete();
			return $res;
		}else{
			return false;
		}
		
	}
}